package nc.util.erm.expamortize;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.erm.util.ErAccperiodUtil;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.fi.pub.Currency;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.md.persist.framework.MDPersistenceService;
import nc.pubitf.erm.expamortize.IExpAmortizeprocQuery;
import nc.util.erm.costshare.ErmForCShareUtil;
import nc.vo.arap.pub.BillEnumCollection;
import nc.vo.arap.pub.BillEnumCollection.FromSystem;
import nc.vo.bd.period2.AccperiodmonthVO;
import nc.vo.ep.bx.BXBusItemVO;
import nc.vo.ep.bx.JKBXHeaderVO;
import nc.vo.ep.bx.JKBXVO;
import nc.vo.erm.costshare.CShareDetailVO;
import nc.vo.erm.expamortize.AggExpamtinfoVO;
import nc.vo.erm.expamortize.ExpamtDetailVO;
import nc.vo.erm.expamortize.ExpamtinfoVO;
import nc.vo.erm.expamortize.ExpamtprocVO;
import nc.vo.erm.util.VOUtils;
import nc.vo.erm.yycommon.YYCommon;
import nc.vo.fipub.exception.ExceptionHandler;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import nc.vo.util.AuditInfoUtil;



public class ExpamtUtil
{
	public ExpamtUtil() {}

	public static String[] getHeadFieldsFromBxVo()
	{
		return new String[] { "pk_jkbx", "total_period", "start_period", "bzbm", "bbhl", "groupbbhl", "globalbbhl", "bbje", "groupbbje", "globalbbje", "pk_group", "zy" };
	}



	public static String[] getBodyFieldsFromCsBody()
	{
		return new String[] { "pk_costshare", "pk_tradetype", "assume_org", "pk_jkbx", "assume_dept", "pk_pcorg", "pk_iobsclass", "pk_resacostcenter", "jobid", "projecttask", "pk_checkele", "customer", "hbbm", "bzbm", "bbje", "groupbbje", "globalbbje", "bbhl", "groupbbhl", "globalbbhl", "pk_group", "pk_proline", "pk_brand" };
	}






	public static String[] getBodyFieldsFromBusBody()
	{
		return new String[] { "pk_jkbx", "pk_busitem", "bzbm", "bbje", "groupbbje", "globalbbje" };
	}








	public static AggExpamtinfoVO[] getExpamtinfoVosFromBx(JKBXVO[] vos)
	{
		List<AggExpamtinfoVO> result = new ArrayList();

		if (vos != null) {
			for (int i = 0; i < vos.length; i++) {
				if (ErmForCShareUtil.isHasCShare(vos[i])) {
					result.add(getAggExpamtinfosByCs(vos[i]));
				} else {
					result.add(getAggExpamtinfosByBus(vos[i]));
				}
			}
		}

		return (AggExpamtinfoVO[])result.toArray(new AggExpamtinfoVO[0]);
	}






	private static AggExpamtinfoVO getAggExpamtinfosByCs(JKBXVO vo)
	{
		CShareDetailVO[] csChildren = vo.getcShareDetailVo();
		JKBXHeaderVO bxHead = vo.getParentVO();










		List<ExpamtDetailVO> childrenList = new ArrayList();

		AggExpamtinfoVO expamtinfo = new AggExpamtinfoVO();

		ExpamtinfoVO head = new ExpamtinfoVO();
		setExpamtinfoDefaultValue(head, bxHead);
		head.setPk_org(bxHead.getPk_org());

		head.setTotal_amount(bxHead.getYbje());
		head.setBbje(bxHead.getBbje());
		head.setGroupbbje(bxHead.getGroupbbje());
		head.setGlobalbbje(bxHead.getGlobalbbje());

		for (int i = 0; i < csChildren.length; i++) {
			CShareDetailVO cShareDetailVO = csChildren[i];
			if (cShareDetailVO.getAssume_amount().compareTo(UFDouble.ZERO_DBL) > 0)
			{

				ExpamtDetailVO detail = new ExpamtDetailVO();
				copyInfo(detail, cShareDetailVO, getBodyFieldsFromCsBody());

				detail.setPk_cshare_detail(cShareDetailVO.getPk_cshare_detail());
				detail.setTotal_amount(cShareDetailVO.getAssume_amount());
				detail.setTotal_period(head.getTotal_period());
				detail.setRes_period(head.getRes_period());
				detail.setPk_org(head.getPk_org());
				detail.setBzbm(head.getBzbm());
				detail.setCashproj(bxHead.getCashproj());

				setExpamtDetailResAmount(detail);






				childrenList.add(detail);
			}
		}
		setExpamtHeadResAmount(head);

		expamtinfo.setParentVO(head);
		expamtinfo.setChildrenVO((CircularlyAccessibleValueObject[])childrenList.toArray(new ExpamtDetailVO[0]));


		return expamtinfo;
	}

	private static void setExpamtHeadResAmount(ExpamtinfoVO head) {
		head.setRes_amount(head.getTotal_amount());
		head.setRes_orgamount(head.getBbje());
		head.setRes_groupamount(head.getGroupbbje());
		head.setRes_globalamount(head.getGlobalbbje());
	}

	private static void setExpamtDetailResAmount(ExpamtDetailVO detail) {
		detail.setRes_amount(detail.getTotal_amount());
		detail.setRes_orgamount(detail.getBbje());
		detail.setRes_groupamount(detail.getGroupbbje());
		detail.setRes_globalamount(detail.getGlobalbbje());
	}








	private static Map<String, List<CShareDetailVO>> getOrgCsMap(CShareDetailVO[] csChildren)
	{
		Map<String, List<CShareDetailVO>> orgMap = new HashMap();

		for (CShareDetailVO csdetail : csChildren) {
			List<CShareDetailVO> listTemp = (List)orgMap.get(csdetail.getAssume_org());

			if (listTemp == null) {
				listTemp = new ArrayList();
				orgMap.put(csdetail.getAssume_org(), listTemp);
			}

			listTemp.add(csdetail);
		}
		return orgMap;
	}

	private static void copyInfo(SuperVO toVo, SuperVO fromVo, String[] fields) {
		for (String field : fields) {
			toVo.setAttributeValue(field, fromVo.getAttributeValue(field));
		}
	}






	private static AggExpamtinfoVO getAggExpamtinfosByBus(JKBXVO vo)
	{
		AggExpamtinfoVO aggVo = new AggExpamtinfoVO();


		ExpamtinfoVO head = new ExpamtinfoVO();
		JKBXHeaderVO bxHead = vo.getParentVO();

		setExpamtinfoDefaultValue(head, bxHead);
		head.setPk_org(bxHead.getPk_org());

		setExpamtHeadResAmount(head);


		BXBusItemVO[] busBodyVos = vo.getChildrenVO();
		List<ExpamtDetailVO> children = new ArrayList();

		for (int i = 0; i < busBodyVos.length; i++) {
			BXBusItemVO busIetmVo = busBodyVos[i];
			if (busIetmVo.getYbje().compareTo(UFDouble.ZERO_DBL) > 0)
			{


				ExpamtDetailVO detailVo = new ExpamtDetailVO();

				setExpamtDetailDefaultValues(detailVo, bxHead, busIetmVo);

				setExpamtDetailResAmount(detailVo);

				children.add(detailVo);
			}
		}
		aggVo.setParentVO(head);
		aggVo.setChildrenVO((CircularlyAccessibleValueObject[])children.toArray(new ExpamtDetailVO[0]));
		return aggVo;
	}







	private static void setExpamtDetailDefaultValues(ExpamtDetailVO detail, JKBXHeaderVO bxHead, BXBusItemVO busIetmVo)
	{
		copyInfo(detail, busIetmVo, getBodyFieldsFromBusBody());

		detail.setPk_busitem(busIetmVo.getPk_busitem());
		detail.setPk_iobsclass(busIetmVo.getSzxmid());
		detail.setPk_pcorg(busIetmVo.getPk_pcorg());
		detail.setPk_resacostcenter(busIetmVo.getPk_resacostcenter());
		detail.setJobid(busIetmVo.getJobid());
		detail.setProjecttask(busIetmVo.getProjecttask());
		detail.setPk_checkele(busIetmVo.getPk_checkele());
		detail.setTotal_amount(busIetmVo.getYbje());

		detail.setPk_proline(busIetmVo.getPk_proline());
		detail.setPk_brand(busIetmVo.getPk_brand());

		String[] attrNames = busIetmVo.getAttributeNames();

		for (String attr : attrNames) {
			if (attr.startsWith("defitem")) {
				detail.setAttributeValue(attr, busIetmVo.getAttributeValue(attr));
			}
		}


		detail.setPk_org(bxHead.getFydwbm());
		detail.setBzbm(bxHead.getBzbm());
		detail.setAssume_org(bxHead.getFydwbm());
		detail.setAssume_dept(bxHead.getFydeptid());
		detail.setCustomer(bxHead.getCustomer());
		detail.setHbbm(bxHead.getHbbm());
		detail.setCashproj(bxHead.getCashproj());
		detail.setPk_group(bxHead.getPk_group());


		detail.setBbhl(bxHead.getBbhl());
		detail.setGroupbbhl(bxHead.getGroupbbhl());
		detail.setGlobalbbhl(bxHead.getGlobalbbhl());

		detail.setTotal_period(bxHead.getTotal_period());
		detail.setRes_period(bxHead.getTotal_period());
	}






	private static void setExpamtinfoDefaultValue(ExpamtinfoVO head, JKBXHeaderVO bxHead)
	{
		copyInfo(head, bxHead, getHeadFieldsFromBxVo());

		head.setBx_billno(bxHead.getDjbh());
		head.setTotal_amount(bxHead.getYbje());
		head.setRes_period(head.getTotal_period());
		head.setBx_deptid(bxHead.getDeptid());
		head.setBx_dwbm(bxHead.getDwbm());
		head.setBx_jkbxr(bxHead.getJkbxr());
		head.setBx_pk_org(bxHead.getPk_org());
		head.setBx_pk_billtype(bxHead.getDjlxbm());
		head.setBx_djrq(bxHead.getDjrq());

		head.setBillstatus(Integer.valueOf(0));
		head.setPk_billtype("266X");

		String[] attrNames = bxHead.getAttributeNames();

		for (String attr : attrNames) {
			if (attr.startsWith("zyx")) {
				String num = attr.substring("zyx".length());
				head.setAttributeValue("defitem" + num, bxHead.getAttributeValue(attr));
			}
		}
	}








	public static void addComputePropertys(AggExpamtinfoVO[] expamtInfoVos, String currentAccMonth)
			throws BusinessException
			{
		if ((expamtInfoVos == null) || (expamtInfoVos.length == 0)) {
			return;
		}

		String pk_org = ((ExpamtinfoVO)expamtInfoVos[0].getParentVO()).getPk_org();
		currentAccMonth = ErAccperiodUtil.getAccperiodmonthByAccMonth(pk_org, currentAccMonth).getYearmth();
		try
		{
			for (AggExpamtinfoVO vo : expamtInfoVos) {
				ExpamtinfoVO parentVo = (ExpamtinfoVO)vo.getParentVO();
				ExpamtDetailVO[] children = (ExpamtDetailVO[])vo.getChildrenVO();


				setAmtStatus(parentVo, currentAccMonth);


				setAggVoCurrentComputeInfo(vo, currentAccMonth);


				setAggVoAccumulateExpamtAmount(vo);


				parentVo.setAccu_period(Integer.valueOf(parentVo.getTotal_period().intValue() - parentVo.getRes_period().intValue()));
				if (children != null) {
					for (int i = 0; i < children.length; i++) {
						children[i].setAccu_period(parentVo.getAccu_period());
					}
				}
			}
		}
		catch (Exception e) {
			ExceptionHandler.handleException(e);
		}
			}







	public static void addComputePropertys(ExpamtinfoVO[] expamtInfoVos, String currAccMonth)
			throws BusinessException
			{
		if ((expamtInfoVos == null) || (expamtInfoVos.length <= 0)) {
			return;
		}

		String pk_org = expamtInfoVos[0].getPk_org();
		currAccMonth = ErAccperiodUtil.getAccperiodmonthByAccMonth(pk_org, currAccMonth).getYearmth();

		try
		{
			for (ExpamtinfoVO expamtInfoVo : expamtInfoVos)
			{
				setAmtStatus(expamtInfoVo, currAccMonth);

				setHeadCurrComputeInfo(expamtInfoVo, currAccMonth);

				setHeadAccumulateExpamtAmount(expamtInfoVo);

				expamtInfoVo.setAccu_period(Integer.valueOf(expamtInfoVo.getTotal_period().intValue() - expamtInfoVo.getRes_period().intValue()));
			}
		} catch (Exception e) {
			ExceptionHandler.handleException(e);
		}
			}

	private static void setHeadCurrComputeInfo(ExpamtinfoVO expamtInfoVo, String currAccMonth) throws Exception {
		if ((expamtInfoVo != null) && (currAccMonth != null)) {
			IMDPersistenceQueryService service = MDPersistenceService.lookupPersistenceQueryService();
			AggExpamtinfoVO vo = (AggExpamtinfoVO)service.queryBillOfVOByPK(AggExpamtinfoVO.class, expamtInfoVo.getPk_expamtinfo(), false);


			IExpAmortizeprocQuery procService = (IExpAmortizeprocQuery)NCLocator.getInstance().lookup(IExpAmortizeprocQuery.class);
			ExpamtprocVO[] procVos = procService.queryEffectProcByPksAndAccperiod(VOUtils.getAttributeValues(vo.getChildrenVO(), "pk_expamtdetail"), currAccMonth);


			setHeadCurrComputeInfo(expamtInfoVo, currAccMonth, procVos);
		}
	}





	private static void setAmtStatus(ExpamtinfoVO parentVo, String currAccMonth)
			throws Exception
			{
		if (parentVo != null)
		{
			AccperiodmonthVO currPeriod = ErAccperiodUtil.getAccperiodmonthByAccMonth(parentVo.getPk_org(), currAccMonth);

			UFBoolean status = getAmtStatus(parentVo, currPeriod.getYearmth());
			parentVo.setAmt_status(status);
		}
			}









	public static UFBoolean getAmtStatus(ExpamtinfoVO expamtInfo, String currAccMonth)
			throws BusinessException
			{
		int accPeriod = expamtInfo.getTotal_period().intValue() - expamtInfo.getRes_period().intValue();
		String startPeriod = expamtInfo.getStart_period();
		String pk_org = expamtInfo.getPk_org();

		UFBoolean status = UFBoolean.FALSE;
		if (expamtInfo.getRes_period().intValue() > 0) {
			if (accPeriod == 0) {
				return UFBoolean.FALSE;
			}

			AccperiodmonthVO lastExpamPeriod = ErAccperiodUtil.getAddAccperiodmonth(pk_org, startPeriod, accPeriod);
			if (currAccMonth.compareTo(lastExpamPeriod.getYearmth()) <= 0) {
				status = UFBoolean.TRUE;
			}
		} else {
			status = UFBoolean.TRUE;
		}
		return status;
			}








	public static void setAggVoCurrentComputeInfo(AggExpamtinfoVO vo, String currentAccMonth)
			throws Exception
			{
		if (vo != null) {
			IExpAmortizeprocQuery service = (IExpAmortizeprocQuery)NCLocator.getInstance().lookup(IExpAmortizeprocQuery.class);

			ExpamtprocVO[] procVos = service.queryEffectProcByPksAndAccperiod(VOUtils.getAttributeValues(vo.getChildrenVO(), "pk_expamtdetail"), currentAccMonth);


			setHeadCurrComputeInfo((ExpamtinfoVO)vo.getParentVO(), currentAccMonth, procVos);
			setBodyCurrExpamtAmount(vo, currentAccMonth, procVos);
		}
			}






	private static void setBodyCurrExpamtAmount(AggExpamtinfoVO vo, String currentAccMonth, ExpamtprocVO[] procVos)
			throws Exception
			{
		if (vo.getChildrenVO() != null) {
			ExpamtDetailVO[] details = (ExpamtDetailVO[])vo.getChildrenVO();
			ExpamtinfoVO parentVo = (ExpamtinfoVO)vo.getParentVO();

			Map<String, ExpamtprocVO> procMap = new HashMap();

			if (procVos != null) {
				for (ExpamtprocVO proc : procVos) {
					procMap.put(proc.getPk_expamtinfo(), proc);
				}
			}


			UFDouble currAmount = parentVo.getCurr_amount();
			currAmount = currAmount == null ? UFDouble.ZERO_DBL : currAmount;
			UFDouble totalAmount = parentVo.getTotal_amount();

			UFDouble restAmount = new UFDouble(currAmount.getDouble());
			for (int i = 0; i < details.length; i++) {
				ExpamtDetailVO detail = details[i];



				ExpamtprocVO expamtprocVO = (ExpamtprocVO)procMap.get(detail.getPk_expamtdetail());
				if (expamtprocVO != null) {
					detail.setCurr_amount(expamtprocVO.getCurr_amount());
					detail.setCurr_orgamount(expamtprocVO.getCurr_orgamount());
					detail.setCurr_groupamount(expamtprocVO.getCurr_groupamount());
					detail.setCurr_globalamount(expamtprocVO.getCurr_globalamount());
				}
				else
				{
					UFDouble resAmount = detail.getRes_amount();
					UFDouble detailTotalAmount = detail.getTotal_amount();
					Integer resPeriod = parentVo.getRes_period();


					UFDouble detailCurrAmount = UFDouble.ZERO_DBL;

					if (resPeriod.intValue() == 1) {
						detailCurrAmount = resAmount;
					}
					else if (i == details.length - 1) {
						detailCurrAmount = restAmount;
						restAmount = UFDouble.ZERO_DBL;


					}
					else
					{

						detailCurrAmount = Currency.getFormaUfValue(detail.getBzbm(), detailTotalAmount.div(totalAmount).multiply(currAmount));
						restAmount = restAmount.sub(detailCurrAmount);
					}

					setCurrentAmount(detail, detailCurrAmount);
				}
			}
			boolean isyycommon = YYCommon.isYyCommon;
			if (isyycommon)
			{
				UFDouble currtaxlessAmount = parentVo.getCurr_taxlessamount();
				UFDouble totaltaxlessAmount = parentVo.getTotal_taxlessamount();
				UFDouble resttaxlessAmount = new UFDouble(parentVo.getCurr_taxlessamount().getDouble());
				for (int i = 0; i < details.length; i++) {
					ExpamtDetailVO detail = details[i];

					ExpamtprocVO expamtprocVO = (ExpamtprocVO)procMap.get(detail.getPk_expamtdetail());
					if (expamtprocVO != null) {
						detail.setCurr_taxlessamount(expamtprocVO.getCurr_taxlessamount());
					}
					else
					{
						UFDouble restaxlessAmount = detail.getRes_taxlessamount();
						UFDouble detailTotaltaxlessAmount = detail.getTotal_taxlessamount();
						Integer resPeriod = parentVo.getRes_period();


						UFDouble detailCurrtaxlessAmount = UFDouble.ZERO_DBL;

						if (resPeriod.intValue() == 1) {
							detailCurrtaxlessAmount = restaxlessAmount;
						}
						else if (i == details.length - 1) {
							detailCurrtaxlessAmount = resttaxlessAmount;
							resttaxlessAmount = UFDouble.ZERO_DBL;
						} else {
							detailCurrtaxlessAmount = Currency.getFormaUfValue(detail.getBzbm(), detailTotaltaxlessAmount.div(totaltaxlessAmount).multiply(currtaxlessAmount));
							restAmount = restAmount.sub(detailCurrtaxlessAmount);
						}

						setCurrenttaxlessAmount(detail, detailCurrtaxlessAmount);
					}
				}
			}
		}
			}


	private static void setCurrentAmount(ExpamtDetailVO vo, UFDouble currAmount)
			throws Exception
			{
		currAmount = Currency.getFormaUfValue(vo.getBzbm(), currAmount);

		if (currAmount == null) {
			currAmount = UFDouble.ZERO_DBL;
		}
		vo.setCurr_amount(currAmount);


		UFDouble[] bbJes = getOriAmountsBy(currAmount, vo.getPk_org(), vo.getBzbm(), vo.getPk_group(), vo.getBbhl(), vo.getGroupbbhl(), vo.getGlobalbbhl(), new UFDate());


		vo.setCurr_orgamount(bbJes[0]);
		vo.setCurr_groupamount(bbJes[1]);
		vo.setCurr_globalamount(bbJes[2]);
			}

	private static void setCurrenttaxlessAmount(ExpamtDetailVO vo, UFDouble currtaxlessAmount)
			throws Exception
			{
		currtaxlessAmount = Currency.getFormaUfValue(vo.getBzbm(), currtaxlessAmount);
		if (currtaxlessAmount == null) {
			currtaxlessAmount = UFDouble.ZERO_DBL;
		}
		vo.setCurr_taxlessamount(currtaxlessAmount);
			}








	public static void setHeadCurrComputeInfo(ExpamtinfoVO expamtInfo, String currAccMonth, ExpamtprocVO[] procVos)
			throws Exception
			{
		if (expamtInfo != null) {
			Integer totalperiod = Integer.valueOf(expamtInfo.getTotal_period() == null ? 0 : expamtInfo.getTotal_period().intValue());
			Integer resPeriod = expamtInfo.getRes_period();
			UFDouble resAmount = expamtInfo.getRes_amount();

			if (resAmount == null) {
				resAmount = UFDouble.ZERO_DBL;
			}


			UFDouble currAmount = UFDouble.ZERO_DBL;

			if ((procVos != null) && (procVos.length > 0))
			{
				for (ExpamtprocVO proc : procVos) {
					currAmount = currAmount.add(proc.getCurr_amount());
				}
				expamtInfo.setAmortize_date(procVos[0].getAmortize_date());
				expamtInfo.setAmortize_user(procVos[0].getAmortize_user());
			} else {
				if (resPeriod.intValue() == 1) {
					currAmount = resAmount;
				} else if (resPeriod.intValue() == 0) {
					currAmount = UFDouble.ZERO_DBL;
				} else if ((expamtInfo.getCurr_amount() != null) && (expamtInfo.getCurr_amount().compareTo(UFDouble.ZERO_DBL) > 0)) {
					currAmount = expamtInfo.getCurr_amount();
				} else if ((expamtInfo.getCurr_amount() == null) || (expamtInfo.getCurr_amount().compareTo(UFDouble.ZERO_DBL) == 0)) {
					currAmount = expamtInfo.getCurr_amount();
				} else {
					String srcsystem = expamtInfo.getSrcsystem();
					if (((srcsystem != null) && (BillEnumCollection.FromSystem.AR.VALUE.toString().equalsIgnoreCase(srcsystem))) || ((srcsystem != null) && (BillEnumCollection.FromSystem.AP.VALUE.toString().equalsIgnoreCase(srcsystem))))
					{
						if ((totalperiod != null) && (resPeriod != null) && (resPeriod == totalperiod)) {
							UFDouble fistperiodbbje = expamtInfo.getFistperiodbbje() == null ? UFDouble.ZERO_DBL : expamtInfo.getFistperiodbbje();
							if (fistperiodbbje.compareTo(UFDouble.ZERO_DBL) <= 0) {
								currAmount = resAmount.div(resPeriod.intValue());
							} else {
								currAmount = fistperiodbbje;
							}
						} else {
							currAmount = resAmount.div(resPeriod.intValue());
						}
					} else {
						currAmount = resAmount.div(resPeriod.intValue());
					}
				}

				expamtInfo.setAmortize_user(AuditInfoUtil.getCurrentUser());
				expamtInfo.setAmortize_date(new UFDate(InvocationInfoProxy.getInstance().getBizDateTime()));
			}

//			if (!UFDouble.ZERO_DBL.equals(currAmount))
//			{
				currAmount = Currency.getFormaUfValue(expamtInfo.getBzbm(), currAmount);
				expamtInfo.setCurr_amount(currAmount);
				UFDouble[] bbJes = getOriAmountsBy(currAmount, expamtInfo.getPk_org(), expamtInfo.getBzbm(), expamtInfo.getPk_group(), expamtInfo.getBbhl(), expamtInfo.getGroupbbhl(), expamtInfo.getGlobalbbhl(), expamtInfo.getCreationtime() == null ? new UFDate() : expamtInfo.getCreationtime().getDate());
				expamtInfo.setCurr_orgamount(bbJes[0]);
				expamtInfo.setCurr_groupamount(bbJes[1]);
				expamtInfo.setCurr_globalamount(bbJes[2]);
//			}

			boolean isyycommon = YYCommon.isYyCommon;
			if (isyycommon) {
				UFDouble restaxlessAmount = expamtInfo.getRes_taxlessamount();
				if (restaxlessAmount == null) {
					restaxlessAmount = UFDouble.ZERO_DBL;
				}
				UFDouble currtaxlessAmount = UFDouble.ZERO_DBL;
				if ((procVos != null) && (procVos.length > 0))
				{
					for (ExpamtprocVO proc : procVos) {
						currtaxlessAmount = currtaxlessAmount.add(proc.getCurr_taxlessamount());
					}
				}
				else if (resPeriod.intValue() == 1) {
					currtaxlessAmount = restaxlessAmount;
				} else if (resPeriod.intValue() == 0) {
					currtaxlessAmount = UFDouble.ZERO_DBL;
				} else if ((expamtInfo.getCurr_taxlessamount() != null) && (expamtInfo.getCurr_taxlessamount().compareTo(UFDouble.ZERO_DBL) > 0)) {
					currtaxlessAmount = expamtInfo.getCurr_taxlessamount();
				} else {
					String srcsystem = expamtInfo.getSrcsystem();
					if (((srcsystem != null) && (BillEnumCollection.FromSystem.AR.VALUE.toString().equalsIgnoreCase(srcsystem))) || ((srcsystem != null) && (BillEnumCollection.FromSystem.AP.VALUE.toString().equalsIgnoreCase(srcsystem))))
					{
						if ((totalperiod != null) && (resPeriod != null) && (resPeriod == totalperiod)) {
							UFDouble fistperiodtaxlessbbje = expamtInfo.getFistperiodtaxlessbbje() == null ? UFDouble.ZERO_DBL : expamtInfo.getFistperiodtaxlessbbje();
							if (fistperiodtaxlessbbje.compareTo(UFDouble.ZERO_DBL) <= 0) {
								currtaxlessAmount = restaxlessAmount.div(resPeriod.intValue());
							} else {
								currtaxlessAmount = fistperiodtaxlessbbje;
							}
						} else {
							currtaxlessAmount = restaxlessAmount.div(resPeriod.intValue());
						}
					} else {
						currtaxlessAmount = restaxlessAmount.div(resPeriod.intValue());
					}
				}


				if (!UFDouble.ZERO_DBL.equals(currtaxlessAmount))
				{
					currtaxlessAmount = Currency.getFormaUfValue(expamtInfo.getBzbm(), currtaxlessAmount);
					expamtInfo.setCurr_taxlessamount(currtaxlessAmount);
				}
			}
		}
			}



	public static void setAggVoAccumulateExpamtAmount(AggExpamtinfoVO vo)
	{
		if (vo != null) {
			setHeadAccumulateExpamtAmount((ExpamtinfoVO)vo.getParentVO());
			setBodyAccumulateExpamtAmount((ExpamtDetailVO[])vo.getChildrenVO());
		}
	}





	public static void setHeadAccumulateExpamtAmount(ExpamtinfoVO vo)
	{
		if (vo != null) {
			UFDouble amount = vo.getTotal_amount();
			UFDouble bbje = vo.getBbje();
			UFDouble groupBbje = vo.getGroupbbje();
			UFDouble globalBbje = vo.getGlobalbbje();

			vo.setAccu_amount(getSubAmount(amount, vo.getRes_amount()));
			vo.setAccu_orgamount(getSubAmount(bbje, vo.getRes_orgamount()));
			vo.setAccu_groupamount(getSubAmount(groupBbje, vo.getRes_groupamount()));
			vo.setAccu_globalamount(getSubAmount(globalBbje, vo.getRes_globalamount()));

			boolean isyycommon = YYCommon.isYyCommon;
			if (isyycommon) {
				UFDouble taxlessamount = vo.getTotal_taxlessamount();
				vo.setAccu_taxlessamount(getSubAmount(taxlessamount, vo.getRes_taxlessamount()));
			}
		}
	}





	private static void setBodyAccumulateExpamtAmount(ExpamtDetailVO[] vos)
	{
		if (vos != null) {
			for (ExpamtDetailVO vo : vos) {
				UFDouble amount = vo.getTotal_amount();
				UFDouble bbje = vo.getBbje();
				UFDouble groupBbje = vo.getGroupbbje();
				UFDouble globalBbje = vo.getGlobalbbje();

				vo.setAccu_amount(getSubAmount(amount, vo.getRes_amount()));
				vo.setAccu_orgamount(getSubAmount(bbje, vo.getRes_orgamount()));
				vo.setAccu_groupamount(getSubAmount(groupBbje, vo.getRes_groupamount()));
				vo.setAccu_globalamount(getSubAmount(globalBbje, vo.getRes_globalamount()));

				boolean isyycommon = YYCommon.isYyCommon;
				if (isyycommon) {
					UFDouble taxlessamount = vo.getTotal_taxlessamount();
					vo.setAccu_taxlessamount(getSubAmount(taxlessamount, vo.getRes_taxlessamount()));
				}
			}
		}
	}







	private static UFDouble getSubAmount(UFDouble subAmount, UFDouble subedAmount)
	{
		if (subAmount == null) {
			subAmount = UFDouble.ZERO_DBL;
		}

		if (subedAmount == null) {
			subedAmount = UFDouble.ZERO_DBL;
		}

		return subAmount.sub(subedAmount);
	}





















	public static UFDouble[] getOriAmountsBy(UFDouble ybAmount, String pk_org, String bzbm, String pk_group, UFDouble orghl, UFDouble grouphl, UFDouble globalhl, UFDate date)
			throws Exception
			{
		UFDouble[] result = new UFDouble[3];

		UFDouble[] je = Currency.computeYFB(pk_org, 1, bzbm, ybAmount, null, null, null, orghl, date);


		UFDouble[] money = Currency.computeGroupGlobalAmount(je[0], je[2], bzbm, date, pk_org, pk_group, globalhl, grouphl);


		result[0] = je[2];
		result[1] = money[0];
		result[2] = money[1];

		return result;
			}
}